
from data_define import Recode
import json


class FileReader:
    def read_data(self):
        pass

class TextFileReader(FileReader):
    
    def __init__(self,path):
        self.path=path


    #复写父类方法
    def read_data(self):
        recode_list=[]
        f=open(self.path,"r",encoding="UTF-8")
        for line in f.readlines():
            line=line.strip()
            data_list=line.split(",")
            recode=Recode(data_list[0],data_list[1],int(data_list[2]),data_list[3])
            recode_list.append(recode)
            # print(recode_list)
        
        f.close()
        return recode_list
    
class TextFileReaderJSON(FileReader):
    
    def __init__(self,path):
        self.path=path


    #复写父类方法
    def read_data(self):
        recode_list1=[]
        f=open(self.path,"r",encoding="UTF-8")
        for line in f.readlines():
            # line=line.strip()
            line=json.loads(line)
            recode_list_jason=Recode(line["date"],line["order_id"],line["money"],line["province"])
            recode_list1.append(recode_list_jason)

        f.close() 
        return recode_list1
           

if __name__=='__main__':
    text_file_reader=TextFileReader("/home/zjg/2011年一月销售数据")
    list1=text_file_reader.read_data()
    for item in list1:
        print(item)

 

    text_file_reader_jason=TextFileReaderJSON("/home/zjg/2012年二月销售数据json")
    list2=text_file_reader_jason.read_data()
    for item in list2:
        print(item)
    # print(list2)
